<template>
	<section >
		<!--工具条-->
		<el-col :span="24" class="toolbar"  style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model.trim="filters.keyword" placeholder="门店名称或地址"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getShops">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shops" :height="520" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" label="序号" width="70">
			</el-table-column>
			<el-table-column prop="name" label="门店名称" width="180" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="联系电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="开店日期" width="180" sortable>
			</el-table-column>
			<el-table-column prop="address" label="门店地址" width="180" sortable>
			</el-table-column>
			<el-table-column prop="logo" label="门店logo" width="180" sortable>
                <template scope="scope">
					<el-avatar :size="60"  @error="errorHandler">
						<img width="100px"  :src="'http://121.37.194.36' +scope.row.logo" alt="">
					</el-avatar>
                </template>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="180"  sortable>
				<template scope="scope">
					<span v-if="scope.row.state == 0" style="color: #5daf34">正常</span>
					<span v-if="scope.row.state == 1" style="color: #dea726">待审核</span>
					<span v-if="scope.row.state == 2" style="color: #1d8ce0">待激活</span>
					<span v-if="scope.row.state == -1" style="color: #e64242;">已禁用</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="170">
				<template scope="scope">
					<el-button size="small"  @click="handleEdit(scope.$index, scope.row)">审核</el-button>
					<!--:disabled="scope.row.state!=1"-->
				</template>
			</el-table-column>
		</el-table>

		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange" @size-change="handleSizeChange"
						   :total="total"
						   :current-page="page"
						   :page-size="pageSize"
						   :page-sizes="[5, 10, 20, 50]"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--审核界面-->
		<el-dialog title="新增或编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px"  ref="editForm">
				<el-form-item label="店铺Logo">
					<img :src="'http://121.37.194.36' + editForm.logo" style="width: 80px;height: 60px;"/>
				</el-form-item>
				<el-form-item label="店铺名称">
					<span>{{editForm.name}}</span>
				</el-form-item>
				<el-form-item label="联系电话">
					<span>{{editForm.tel}}</span>
				</el-form-item>
				<el-form-item label="状态">
					<span v-if="editForm.state == 0" style="color: #5daf34">正常</span>
					<span v-if="editForm.state == 1" style="color: #dea726">待审核</span>
					<span v-if="editForm.state == 2" style="color: #1d8ce0">待激活</span>
					<span v-if="editForm.state == -1" style="color: #e64242;">已禁用</span>
				</el-form-item>
				<el-form-item label="店铺地址">
					<span>{{editForm.address}}</span>
				</el-form-item>
				<el-form-item label="审核操作">
					<el-radio-group v-model="editForm.type">
						<el-radio :label="1">不通过</el-radio>
						<el-radio :label="2">驳回</el-radio>
						<el-radio :label="3">通过</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
                    keyword: ''
				},
				value:true,
				shops: [],
				total: 0,
				page: 1,
				pageSize:10,
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
                // 前面部分的地址
                frontAddress: '',
                // 后面的细节地址
                afterAddress: '',
				//编辑界面数据
				editForm: {
					id: null,
                    name: '',
                    tel:'',
                    registerTime:'',
                    state: 0,
                    address: '',
                    logo: '',
					admin:'',
					type:3
				},
			}
		},
		methods: {
            //切换当前显示多少条数据
            handleSizeChange(val) {
                this.pageSize = val;
                this.getShops();
            },
            //切换当前页码
			handleCurrentChange(val) {
				this.page = val;
				this.getShops();
			},
            errorHandler() {
                return true
            },
            //获取门店列表
			getShops() {
				let para = {
					currentPage: this.page,
					keyword: this.filters.keyword,
					pageSize:this.pageSize
				};
				this.listLoading = true;
				this.$http.post("/shop",para)
					.then(result=>{
                        this.listLoading = false;
                        result = result.data;//直接抽取了一层
                        if(result){//true
                            this.total = result.totals;
                            this.shops = result.rows;
						}else{//false
                            this.$message({
                                message: '查询数据失败！'+result.message,
                                type: 'error'
                            });
						}
					})
					.catch(result=>{
						alert("系统错误！！！")
                    })
			},
			//显示审核界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
                //this.$refs['editForm'].resetFields();//重置表单内容和表单验证
                this.editForm = Object.assign({}, row);
                this.editForm.type = 3

            },
			//提交审核
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交审核吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
                            this.$http.get("/shop/"+para.id+"/"+para.type)
								.then(result=>{
								    result = result.data;
								    if(result.success){
                                        this.page = 1;
                                        this.editLoading = false;
                                        this.getShops();//刷新页面
                                        this.editFormVisible = false;
									}else {
                                        this.$message({
                                            message: result.message,
                                            type: 'error'
                                        });
									}
								})
								.catch(result=>{
                                    this.editFormVisible = false;
									alert("系统错误")
								})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
		},
		mounted() {
			this.getShops();
		},
		watch:{
		    "filters.keyword"(oldVal,newVal){
				this.oldValue = newVal;
				this.getShops();
			}
		}
	}

</script>

<style scoped>

</style>